package top.huhuiyu.teachproject.service.impl;

import com.github.javafaker.Faker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.huhuiyu.teachproject.base.BaseResult;
import top.huhuiyu.teachproject.dao.TbUserInfoDAO;
import top.huhuiyu.teachproject.entity.TbUser;
import top.huhuiyu.teachproject.entity.TbUserInfo;
import top.huhuiyu.teachproject.service.AuthService;
import top.huhuiyu.teachproject.service.AutoDataService;
import top.huhuiyu.teachproject.utils.PinyinUtil;
import top.huhuiyu.teachproject.utils.Utils;

import java.util.Locale;

@Service
@Slf4j
@RequiredArgsConstructor
public class AutoDataServiceImpl implements AutoDataService {
  private final AuthService authService;
  private final PinyinUtil pinyinUtil;
  private final TbUserInfoDAO tbUserInfoDAO;
  private final Faker faker = new Faker(Locale.CHINA);

  @Override
  public int makeUser() {
    int rand = Utils.rand(-2, 5);
    int count = 0;
    for (int i = 0; i < rand; i++) {
      // 虚拟用户信息
      String name = faker.name().fullName();
      TbUser tbUser = new TbUser();
      tbUser.setUsername(pinyinUtil.getPinYin(name));
      tbUser.setPassword("abcd-1234");
      // 模拟注册
      BaseResult result = authService.reg(tbUser);
      // 成功就修改附加信息
      if (result.isSuccess()) {
        count++;
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setNickname(name);
        tbUserInfo.setPhone(faker.phoneNumber().cellPhone());
        tbUserInfo.setEmail(faker.internet().emailAddress(tbUser.getUsername()));
        tbUserInfo.setUid(tbUser.getUid());
        tbUserInfoDAO.updateById(tbUserInfo);
      }
    }
    return count;
  }
}
